package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.VideoShop;
import com.cl.short_video_java.service.SysSettingService;
import com.cl.short_video_java.service.VideoShopService;
import com.cl.short_video_java.mapper.VideoShopMapper;
import com.cl.short_video_java.util.StringUtils;
import com.cl.short_video_java.vo.shop.ShopAdd;
import com.cl.short_video_java.vo.shop.ShopSearch;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author object
 * @description 针对表【video_shop】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class VideoShopServiceImpl extends ServiceImpl<VideoShopMapper, VideoShop>
		implements VideoShopService {

	private SysSettingService sysSettingService;

	VideoShopServiceImpl(SysSettingService sysSettingService) {
		this.sysSettingService = sysSettingService;
	}

	@Override
	public Page<VideoShop> page(ShopSearch shop) {

		Page<VideoShop> page = this.page(new Page<>(shop.getPageNum(), shop.getPageSize()),
				new QueryWrapper<VideoShop>()
						.like(StringUtils.isNotEmpty(shop.getName()), "title", shop.getName())
						.eq(shop.getDiscount() != null, "discount", shop.getDiscount())
						.eq(shop.getMarketable() != null, "marketable", shop.getMarketable())
						.eq("is_delete", 0)
						.orderByAsc("sort")
		);
		return page;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void limit(Integer id) {
		VideoShop one = this.getOne(new QueryWrapper<VideoShop>().eq("id", id));
		if (one.getMarketable() == 1) {
			one.setMarketable(0);
		} else {
			one.setMarketable(1);
		}

		this.updateById(one);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void del(List<Integer> ids) {
		for (Integer id : ids) {
			this.update(new UpdateWrapper<VideoShop>().eq("id", id).set("is_delete", 1));
		}
	}

	@Override
	public void save(ShopAdd shopAdd) {
		System.out.println("新增商品==============================================" + shopAdd);
		VideoShop videoShop = new VideoShop();
		if (shopAdd.getId() != null) {
			videoShop = this.getById(shopAdd.getId());
		}
		videoShop.setTitle(shopAdd.getTitle());
		videoShop.setDesc(shopAdd.getDesc());
		videoShop.setMarketable(shopAdd.getMarketable());
		videoShop.setSort(shopAdd.getSort());
		videoShop.setImgUrl(shopAdd.getImgUrl());
		videoShop.setDiscount(Double.valueOf(shopAdd.getDiscount()));
		videoShop.setPrice(BigDecimal.valueOf(shopAdd.getPrice()));
		videoShop.setType(shopAdd.getType());
		if (videoShop.getType() == 1) {
			videoShop.setDate(shopAdd.getDate() == null ? 1 : shopAdd.getDate());
			videoShop.setDateUnit(shopAdd.getDateUnit() == null ? 2 : shopAdd.getDateUnit());
		}
		this.saveOrUpdate(videoShop);
	}


	/**
	 * 获取会员商品
	 *
	 * @return
	 */
	@Override
	public List<VideoShop> getVipList() {
		List<VideoShop> list = this.list(new QueryWrapper<VideoShop>()
				.select("id", "title", "`desc`", "price", "discount", "img_url", "date", "date_unit", "type", "sort")
				.eq("is_delete", 0)
				.eq("marketable", 1)
				.eq("type", 1).orderByAsc("sort")
		);
		return list;

	}

	@Override
	public List<VideoShop> getCommonList() {
		List<VideoShop> list = this.list(new QueryWrapper<VideoShop>()
				.select("id", "title", "`desc`", "price", "discount", "img_url", "date", "date_unit", "type", "sort")
				.eq("is_delete", 0)
				.eq("marketable", 1)
				.eq("type", 2).orderByAsc("sort")
		);
		Integer rate = Integer.valueOf(sysSettingService.getSysSettingByKey("sys_exchange_rate"));
		for (VideoShop videoShop : list) {
			BigDecimal price = videoShop.getPrice();
			BigDecimal bigDecimal = new BigDecimal(videoShop.getDiscount());
			videoShop.setPayPrice(price.multiply(bigDecimal));
			double v = price.doubleValue() * rate;
			videoShop.setGold((int) v);
		}
		return list;
	}
}




